package com.cskaoyan.service.admin.region;

import com.cskaoyan.bean.MarketRegion;
import com.cskaoyan.bean.MarketRegionExample;
import com.cskaoyan.bean.vo.admin.region.RegionListVo;
import com.cskaoyan.bean.vo.admin.region.RegionListVo2;
import com.cskaoyan.mapper.MarketRegionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: bwj
 * @Date: 2022/09/06/21:59
 * @Description:
 */
@Service
public class RegionServiceImpl implements RegionService  {
    @Autowired
    MarketRegionMapper marketRegionMapper;

    /**
    * @Author: 宾文杰 @Description: 根据当前页获取全部的行政区信息 @Date: 2022/9/6
    */
    @Override
    public RegionListVo list() {
        RegionListVo RegionListVo = new RegionListVo();
        //pid为0的是省级行政区
        MarketRegionExample marketRegionExample = new MarketRegionExample();
        MarketRegionExample.Criteria criteria = marketRegionExample.createCriteria();
        criteria.andPidEqualTo(0);
        List<MarketRegion> marketRegions = marketRegionMapper.selectByExample(marketRegionExample);
        ArrayList<RegionListVo2> regionListVo2s1 = new ArrayList<>();
        for (MarketRegion marketRegion : marketRegions) {
            //根据pid和省级的id相同，选出市级，
            RegionListVo2 regionListVo21 = new RegionListVo2();
            regionListVo21.setCode(marketRegion.getCode());
            regionListVo21.setId(marketRegion.getId());
            regionListVo21.setName(marketRegion.getName());
            regionListVo21.setType(marketRegion.getType());

            Integer pid = marketRegion.getId();
            MarketRegionExample marketRegionExample1 = new MarketRegionExample();
            MarketRegionExample.Criteria criteria1 = marketRegionExample1.createCriteria();
            criteria1.andPidEqualTo(pid);
            List<MarketRegion> marketRegions1 = marketRegionMapper.selectByExample(marketRegionExample1);
            ArrayList<RegionListVo2> regionListVo2s = new ArrayList<>();
            for (MarketRegion region : marketRegions1) {
                //根据区级pid=市级id，选出区级
                RegionListVo2 regionListVo2 = new RegionListVo2();
                regionListVo2.setCode(region.getCode());
                regionListVo2.setId(region.getId());
                regionListVo2.setName(region.getName());
                regionListVo2.setType(region.getType());

                Integer ppid = region.getId();
                MarketRegionExample marketRegionExample2 = new MarketRegionExample();
                MarketRegionExample.Criteria criteria2 = marketRegionExample2.createCriteria();
                criteria2.andPidEqualTo(ppid);
//                criteria2.andPidIsNull();
                List<MarketRegion> marketRegions2 = marketRegionMapper.selectByExample(marketRegionExample2);
                regionListVo2.setChildren(marketRegions2.toArray());
                regionListVo2s.add(regionListVo2);
            }
            regionListVo21.setChildren(regionListVo2s.toArray());
            regionListVo2s1.add(regionListVo21);
        }
        RegionListVo.setTotal(regionListVo2s1.size());
        RegionListVo.setPages(1);
        RegionListVo.setPage(1);
        RegionListVo.setLimit(regionListVo2s1.size());
        RegionListVo.setList(regionListVo2s1.toArray());
        return RegionListVo;
    }
}
